package com.zhangtai.modules.service.impl.regular;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangtai.modules.dao.RegularBpmDao;
import com.zhangtai.modules.entity.regular.RegularBpmEntity;
import com.zhangtai.modules.service.regular.RegularBpmService;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;


@RestController
public class RegularBpmServiceImpl extends ServiceImpl<RegularBpmDao, RegularBpmEntity> implements RegularBpmService {

    @Override
    public PageResult queryPage(@RequestParam(value = "keyword", required = false) String keyword,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        IPage<RegularBpmEntity> page = this.page(
                new Page<RegularBpmEntity>(pageNum, pageSize),
                new QueryWrapper<RegularBpmEntity>().like(!StringUtils.isEmpty(keyword), "name", keyword)
                        .eq("is_delete", 0)
        );

        return new PageResult(page);
    }

    @Override
    public PageResult<RegularBpmEntity> getRegularList(@RequestParam(value = "type", required = false) List<Integer> type,
                                                       @RequestParam(value = "startDate", required = false) Date startDate,
                                                       @RequestParam(value = "name", required = false) String name,
                                                       @RequestParam(value = "code", required = false) String code,
                                                       @RequestParam(value = "endDate", required = false) Date endDate,
                                                       @RequestParam(value = "oid", defaultValue = "1") Long oid,
                                                       @RequestParam(value = "isRemind") boolean isRemind,
                                                       @RequestParam(value = "orgIds") List<Long> orgIds,
                                                       @RequestParam(value = "regularBpmStatus", required = false) List<Integer> regularBpmStatus,
                                                       @RequestParam(value = "orgId") List<Long> orgId,
                                                       @RequestParam(value = "unitId") List<Long> unitId,
                                                       @RequestParam(value = "jobId") List<Long> jobId,
                                                       @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        QueryWrapper<RegularBpmEntity> queryWrapper = new QueryWrapper<RegularBpmEntity>().in(!CollectionUtils.isEmpty(type), "regular_type", type)
                .between(!ObjectUtils.isEmpty(startDate) && !ObjectUtils.isEmpty(endDate), "regular_day", startDate, endDate)
                .like(!StringUtils.isEmpty(name), "name", name)
                .eq(!StringUtils.isEmpty(code), "code", code)
                .eq(!ObjectUtils.isEmpty(oid), "oid", oid)
                .eq(isRemind, "is_regular_remind", 1)
                .in(!CollectionUtils.isEmpty(regularBpmStatus), "regular_bpm_status", regularBpmStatus)
                .eq("is_delete", 0)
                .in(!CollectionUtils.isEmpty(orgId), "before_org_id", orgId)
                .eq(orgId != null && orgId.size() == 0, "before_org_id", null)
                .in(!CollectionUtils.isEmpty(unitId), "before_unit_id", unitId)
                .eq(unitId != null && unitId.size() == 0, "before_unit_id", null)
                .in(!CollectionUtils.isEmpty(jobId), "before_job_id", jobId)
                .eq(jobId != null && jobId.size() == 0, "before_job_id", null);


//       queryWrapper.and(wra->wra.in(!CollectionUtils.isEmpty(orgIds),"unit_id",orgIds).or().
//               eq(CollectionUtils.isEmpty(orgIds)&&ObjectUtils.isEmpty(code),"unit_id",null))

        queryWrapper.and(ObjectUtils.isEmpty(oid), wra -> wra.in(!CollectionUtils.isEmpty(orgIds), "unit_id", orgIds).or().
                eq(CollectionUtils.isEmpty(orgIds), "unit_id", null))
                .orderByAsc("regular_day");

        IPage<RegularBpmEntity> page = this.page(
                new Page<RegularBpmEntity>(pageNum, pageSize),
                queryWrapper
        );
        return new PageResult(page);
    }

    @Override
    public RegularBpmEntity getByIdE(Long id) {
        RegularBpmEntity one = this.getOne(new QueryWrapper<RegularBpmEntity>().eq("id", id).eq("is_delete", 0));

        return one;
    }

    @Override
    public R saveE(RegularBpmEntity entity) {
        boolean b = this.save(entity);
        if (!b) {
            return R.error();
        }
        return R.ok();

    }

    @Override
    public R updateByIdE(RegularBpmEntity entity) {
        boolean b = this.updateById(entity);
        if (!b) {
            return R.error();
        }
        return R.ok();
    }

    @Override
    public R removeOneById(Long id) {
        RegularBpmEntity entity = new RegularBpmEntity();
        entity.setId(id);
        entity.setIsDelete(1);
        boolean b = this.updateById(entity);
        if (!b) {
            return R.error();
        }
        return R.ok();
    }

    @Override
    @Transactional
    public R removeByIdsE(List<Long> ids) {
        for (Long id : ids) {
            R r = removeOneById(id);
            if (!r.getFlag()) {
                return r;
            }
        }
        return R.ok();
    }

}